<template>
  <div class="user-container">
    <!-- 用户基本信息面板 -->
    <div class="user-card">
      <!-- 用户头像、姓名 -->
      <van-cell v-if="user.id">
        <!-- 使用 title 插槽来自定义标题 -->
        <template #icon>
          <img src="../../assets/logo.png" alt="" class="avatar" />
        </template>
        <template #title>
          <span class="username">{{ user.userName }}</span>
        </template>
        <template #label>
          <van-tag color="#fff" text-color="#007bff">申请认证</van-tag>
        </template>
      </van-cell>
      <van-cell v-else>
        <!-- 使用 title 插槽来自定义标题 -->
        <template #icon>
          <img src="../../assets/default.png" alt="" class="avatar" />
        </template>
        <template #title>
          <span class="username">未登录</span>
        </template>
        <template #label>
          <router-link color="#fff" text-color="#007bff" to="/login"
            >点击登录</router-link
          >
        </template>
      </van-cell>
      <!-- 动态、关注、粉丝 -->
      <div class="user-data"></div>
    </div>
    <van-cell>
      <van-row type="flex" justify="center" align="center">
        <van-col span="8" type="flex" justify="center" align="center">
          <router-link to="/hist">
            <van-row><van-icon name="flower-o" color="#07c160" /></van-row>
            <van-row><span>浏览历史</span></van-row>
          </router-link>
        </van-col>
        <van-col span="8" type="flex" justify="center" align="center">
          <router-link to="/choiceLabel">
            <van-row><van-icon name="orders-o" color="#07c160" /></van-row>
            <van-row><span>个性化标签</span></van-row>
          </router-link>
        </van-col>
      </van-row>
    </van-cell>
    <!-- 操作面板 -->
    <van-cell-group class="action-card">
      <van-cell icon="edit" title="编辑资料" is-link to="/user-edit"/>
      <van-cell icon="setting-o" title="重置密码" is-link />
      <van-cell icon="warning-o" title="退出登录" is-link @click="logOut" />
    </van-cell-group>
  </div>
</template>

<script>
import { userDetail } from "@/api/userAPI";
import router from "@/router";
import store from "@/store";
import { Toast } from "vant";

export default {
  name: "User",
  data() {
    return {
      user: {
        id: "",
        standing: false,
        userName: "",
        icon: "",
      },
    };
  },
  created() {
    this.initUserInfo();
  },
  methods: {
    async initUserInfo() {
      // 需要基于 dish-id 发送请求，获取编辑对应的详情数据，进行回显
      const {
        data: { success = false, errorMsg = "", data: res, total = 0 },
      } = await userDetail();
      for (let key in res) {
        this.user[key] = res[key];
      }
    },
    logOut() {
      store.commit("mRemoveTokenInfo");
      // initUserInfo();
      Toast("退出登录");
    },
  },
};
</script>

<style lang="less" scoped>
.user-container {
  .user-card {
    background-color: #07c160;
    color: white;
    padding-top: 20px;
    .van-cell {
      background: #07c160;
      color: white;
      &::after {
        display: none;
      }
      .avatar {
        width: 60px;
        height: 60px;
        background-color: #fff;
        border-radius: 50%;
        margin-right: 10px;
      }
      .username {
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
  .user-data {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    font-size: 14px;
    padding: 30px 0;
    .user-data-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 33.33%;
    }
  }
}
// .action-form {
//   display: flex;
//   justify-content: center;
//   flex-direction: row;
// }
</style>